<template>
  <div class="cart">
    <!-- 头部 -->
    <myhander titelName="购物车"></myhander>
    <div class="empty-cart"  v-if="cartListPage.length ===0">
      <img src="" alt="" class="empty-cart-img">
      <div class="empty-cart-text">购物车还是空的</div>
      <div class="btn">去逛逛</div>
    </div>
    <div class="cart-container" v-else v-for="(item, index) in cartListPage" :key="index">
      <span class="iconfont toogle" :class="item.selectState?'icon-checkbox-marked-circ':'icon-checkbox-blank-circle-outline'" @click="toggleState(index)"></span>
      <div class="item-detail">
        <img :src="item.img" :alt="item.title">
        <div class="item-info">
          <h3>{{item.title}}</h3>
          <div class="pay">
            <div class="pay-price">￥{{item.price}}</div>
            <div class="edit-quantity">
              <p class="operate-btn iconfont icon-minus" @click="handleMinus(index)"></p>
              <p class="btn-input">{{item.num}}</p>
              <p class="operate-btn iconfont icon-plus" @click="handlePlus(index)"></p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="cart-footer">
      <div class="cart-footer-left"><span class="iconfont toogle" :class="isSelectAll?'icon-checkbox-marked-circ':'icon-checkbox-blank-circle-outline'" @click="toggleAll"></span><span>全选</span></div>
      <div class="cart-footer-center"><span>合计：</span><span class="total-price">￥{{totalPrice}}</span></div>
      <div class="cart-footer-right"><span class="goto-pay">结算</span></div>
    </div>
  </div>
</template>

<script>
import myhander from '@/components/myhander'
import { MessageBox } from 'mint-ui'
export default {
  components: {
    myhander
  },
  data () {
    return {
      cartListPage: []
    }
  },
  created () {
    // 页面加载就执行获取本地数据
    this.cartListPage = JSON.parse(localStorage.getItem('cartList') || '[]')
  },
  // 讲操作之后的数据更新回本地
  beforeDestroy () {
    localStorage.setItem('cartList', JSON.stringify(this.cartListPage))
  },
  computed: {
    // 创建总数量计算属性
    totalNum () {
      let total = 0
      // 判断被选中状态
      this.cartListPage.map(item => {
        if (item.selectState) {
          total += item.num
        }
      })
      return total
    },
    // 创建总价计算属性
    totalPrice () {
      let allPrice = 0
      this.cartListPage.map(item => {
        if (item.selectState) {
          allPrice += item.num * item.price
        }
      })
      return allPrice
    },
    // 全局状态切换计算属性
    isSelectAll () {
      let selectAll = true
      this.cartListPage.map(item => {
        // 判断里面的每一项状态
        if (item.selectState === false) {
          selectAll = false
        }
      })
      return selectAll
    }
  },
  methods: {
    toggleState (idx) {
      this.cartListPage[idx].selectState = !this.cartListPage[idx].selectState
    },
    // 切换总状态
    toggleAll () {
      let allStatus = this.isSelectAll
      if (allStatus) {
        this.cartListPage.map(item => {
          item.selectState = false
        })
      } else {
        this.cartListPage.map(item => {
          item.selectState = true
        })
      }
    },
    // 数量减少
    handleMinus (idx) {
      if (this.cartListPage[idx].num === 1) {
        // 提示是否要删除
        MessageBox({
          title: '提示',
          message: '确定删除该商品?',
          showCancelButton: true
        })
          .then(action => {
            if (action === 'confirm') {
              // 删除当前项
              this.cartListPage.splice(idx, 1)
            }
          })
      } else {
        this.cartListPage[idx].num -= 1
      }
    },
    handlePlus (idx) {
      this.cartListPage[idx].num += 1
    }
  }
}
</script>

<style lang="scss" scoped>
  .cart {
  height: 320px;
  margin-top: 45px;
  font-size: 14px;
  .empty-cart {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    height: 100%;
    .empty-cart-img {
      height: 90px;
      width: 90px;
    }
    .empty-cart-text {
      font-size: 20px;
      color: #999;
      padding: 15px 0;
    }
    .btn {
      font-size: 20px;
      padding: 15px 55px;
      text-align: center;
      margin: 0 auto;
      border-radius: 10px;
      background: #ed601b;
      color: #fff;
    }
  }
  .cart-container {
    display: flex;
    align-items: center;
    margin: 5px;
    padding: 8px 10px;
    border: 1px solid #eeeeee;
    border-radius: 20px;
    .toogle {
      width: 20px;
      height: 20px;
      line-height: 20px;
      font-size: 20px;
    }
    .icon-checkbox-marked-circ {
      color: #ff5500;
    }
    .icon-checkbox-blank-circle-outline {
      color: #666666;
    }
  }
  .item-detail {
    display: flex;
    flex: 1;
    img {
      margin: 2px 0 2px 5px;
      width: 90px;
      height: 90px;
    }
    .item-info {
      width: 100%;
      padding: 5px 10px;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      h3 {
        font-size: 16px;
        font-weight: 400;
      }
      .pay {
        display: flex;
        justify-content: space-between;
        .pay-price {
          color: #ff5500;
          font-weight: bold;
          line-height: 30px;
        }
        .edit-quantity {
          display: flex;
          height: 30px;
          line-height: 30px;
          .operate-btn {
            padding: 0 10px;
            font-size: 20px;
            color: #bababa;
          }
          .btn-minus {
            font-size: 14px;
          }
        }
      }
    }
  }
  .cart-footer {
    display: flex;
    justify-content: space-between;
    position: fixed;
    bottom: 0;
    left: 0;
    height: 50px;
    line-height: 50px;
    width: 100%;
    border-top: 1px solid #e7e7e7;
    background-color: #fff;
    .cart-footer-left {
      display: flex;
      justify-content: cneter;
      align-items: center;
      span {
        display: block;
        height: 50px;
        padding: 0 5px;
      }
    }
    .cart-footer-center {
      .total-price {
        color: #ff5500;
        font-weight: bold;
      }
    }
    .cart-footer-right {
      display: flex;
      flex-direction: column;
      padding: 0 20px;
      background-color: #ff5500;
      .goto-pay {
        color: #fff;
      }
    }
    .icon-checkbox-marked-circ {
      color: #ff5500;
    }
    .icon-checkbox-blank-circle-outline {
      color: #666666;
    }
  }
}
</style>
